package arithmetic;

public class Remove_duplicate {
    /*
     * 原地删除数组中的重复项，本质上是将数组中不重复的元素移动到数组左侧 注意：整型数组中无法使用引用，指针使用下标来实现
     */
    public static int removeDuplicates(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        int p = 0;// 慢指针
        int q = 1;// 快指针
        int length = nums.length;
        while (q < length) {
            if (nums[p] == nums[q]) {
                q++;
            } else {
                nums[p + 1] = nums[q];
                p++;
                q++;
            }
        }
        return p + 1;
    }

    public static void main(String[] args) {
        int[] a = { 0, 0, 1, 1, 1, 2, 2, 3, 3, 4 };
        System.out.println("删除前：");
        for (int element : a) {
            System.out.println(element);
        }
        System.out.println("删除后:");
        int len = removeDuplicates(a);
        for (int i = 0; i < len; i++) {
            System.out.print(a[i] + " ");
        }
    }
}